package com.atguigu.spring.service.impl;

import com.atguigu.spring.dao.BookDao;
import com.atguigu.spring.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

/**
 * @Author mo
 * @Date 2024-05-08 09:49:34
 **/
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    //@Transactional
    //@Transactional(readOnly = false)
    //@Transactional(timeout = 3)
    //@Transactional(noRollbackFor = ArithmeticException.class)
    //@Transactional(propagation = Propagation.REQUIRES_NEW)
    @Transactional
    @Override
    public void buyBook(Integer bookId, Integer userId) {
        /*try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        //查询图书的价格
        Integer price = bookDao.getPriceByBookId(bookId);
        //更新图书的库存
        bookDao.updateStock(bookId);
        //更新用户的余额
        bookDao.updateBalance(userId, price);
        //int i = 1 / 0;
    }
}
